package com.techwells.learningkites.controller;

import java.util.HashMap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.techwells.learningkites.R;
import com.techwells.learningkites.model.FeedbackViewModel;
import com.techwells.learningkites.model.VideoDetailViewModel;
import com.techwells.learningkites.networkservice.Constant;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.taco.base.CommonBaseActivity;
import com.techwells.taco.networkservice.ServiceMediator;
import com.techwells.taco.tasktool.TaskToken;
import com.techwells.taco.utils.ToastUtils;

public class FeedbackActivity extends CommonBaseActivity implements OnClickListener {
	
	private EditText feedback;
	private TextView submit;
	private EditText verify;
	private ImageView captcha;
	
	private FeedbackViewModel presentModel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);
		
		initTitleBar(R.string.feedback, defaultLeftClickListener, this, R.string.submit);
		initViews();
	}

	private void initViews() {
		feedback = (EditText) findViewById(R.id.feedback);
		verify = (EditText) findViewById(R.id.verify);
		captcha = (ImageView) findViewById(R.id.captcha);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.common_activity_title_right_tv:
			if (!feedback.getText().toString().equals("")) {
				doRequestFeedback();
			}else {
				ToastUtils.show(this, "请输入反馈内容");
			}
			
			break;

		default:
			break;
		}
		
	}


	@Override
	public void alreadyBindBaseViewModel() {
		// TODO Auto-generated method stub
		super.alreadyBindBaseViewModel();
		
		presentModel = (FeedbackViewModel) baseViewModel;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		doRequestCaptcha();
	}
	
	private void doRequestFeedback() {
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("content", feedback.getText().toString());
		argMap.put("verifyType", Constant.verifyType);
		argMap.put("challenge", verify.getText().toString());
		doTask(KitesServiceMediator.SERVICE_DO_FEEDBACK,argMap);
		
	}
	
	private void doRequestCaptcha() {
		doTask(KitesServiceMediator.SERVICE_GET_CAPTCHA,null);
	}
	
	@Override
	public void refreshData(TaskToken token) {
		// TODO Auto-generated method stub
		super.refreshData(token);
		
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_GET_CAPTCHA)) {
			String captchaString = ((FeedbackViewModel)baseViewModel).captcha;
			byte[] decodeByte = Base64.decode(captchaString, 0);
			Bitmap bitmap = BitmapFactory.decodeByteArray(decodeByte, 0, decodeByte.length);
			captcha.setImageBitmap(bitmap);
		}else if (method.equals(KitesServiceMediator.SERVICE_DO_FEEDBACK)) {
			int returnCode = ((FeedbackViewModel)baseViewModel).returnCode;
			if (returnCode == ServiceMediator.Service_Return_Success) {
				ToastUtils.show(this, "提交成功");
				finish();
			}
		}
	}
	
	
	@Override
	public void requestFailedHandle(TaskToken token, int errorCode,
			String errorMsg) {
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_DO_FEEDBACK)) {
			switch (errorCode) {
			case 461:
				ToastUtils.show(this, "验证码错误");
				break;
			case 460:
				ToastUtils.show(this, "验证码失效");
				break;
			case 400:
				ToastUtils.show(this, "请输入验证码");
				break;

			default:
				ToastUtils.show(this, errorMsg);
				break;
			}
		}
	}
}
